package com.cooldatasoft.dao.impl;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.cooldatasoft.base.RootDAOImpl;
import com.cooldatasoft.dao.UserDAO;
import com.cooldatasoft.data.User;

public class UserDAOImpl extends RootDAOImpl<User> implements UserDAO{

	public UserDAOImpl() {
		super(User.class);
	}

	public User authenticate(final User user) {
		
		HibernateCallback hibernateCallback = new HibernateCallback(){

			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery("FROM User WHERE username = ? AND password = ? AND deleted = ?");
				query.setString(0, user.getUsername());
				query.setString(1, user.getPassword());
				query.setBoolean(2, new Boolean(false));
				return query.uniqueResult();
			}
			
		};
		return (User) getHibernateTemplate().execute(hibernateCallback);
	}

}
